package com.bountytask.controller;

import com.alibaba.fastjson.JSONObject;
import com.bountytask.config.WebSocketServer;
import com.bountytask.entity.Task;
import com.bountytask.entity.UserTask;
import com.bountytask.service.InformationService;
import com.bountytask.service.UserAndTaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
@Slf4j
@RestController
public class UserAndTaskController {

    @Autowired
    private UserAndTaskService userAndTaskService;

    @Autowired
    private InformationService informationService;

    @Autowired
    private WebSocketServer webSocketServer;

    //根据用户角色和任务状态查询
    @GetMapping("/task/_mget/{userid}/{role}/{status}")
    public JSONObject queryTaskByStatus(@PathVariable("userid") long userId,
                                        @PathVariable("role") int role,
                                        @PathVariable("status") int status){
        List<Task> tasks=
            userAndTaskService.findTaskByStatus
                (userId,role,status);
        JSONObject resultJson = new JSONObject(true);
        resultJson.put("code",200);
        resultJson.put("message","成功");
        resultJson.put("data",tasks);
        return resultJson;
    }
    //提交任务
    @PostMapping("/task/submit")
    public JSONObject submitTask(@RequestBody JSONObject recData)throws IOException {
        JSONObject resultJson = new JSONObject();
        int flag= userAndTaskService.updateForSubmit(recData.getLong("userid"),recData.getLong("taskid"));
        if (flag==400){
            resultJson.put("code",flag);
            resultJson.put("message","失败");
            return resultJson;
        }
        long posterId=userAndTaskService.getPoster(recData.getLong("taskid"));
        webSocketServer.
                sendMessageList(informationService.
                        getInfo(1, informationService.
                                addInformation(1,2,recData.getLong("taskid"),posterId)),posterId);
        resultJson.put("message","成功");
        resultJson.put("code",flag);
        return resultJson;
    }

    //发布人确认提交的任务
    @PutMapping("/task/confirm")
    public JSONObject confirmTask(@RequestBody JSONObject recData)throws IOException{
        JSONObject resultJson = new JSONObject();
        int flag= userAndTaskService.confirmTask(recData.getLong("userid"),recData.getLong("taskid"));
        if (flag==400){
            resultJson.put("code",flag);
            resultJson.put("message","失败");
            return resultJson;
        }
        webSocketServer.
                sendMessageList(informationService.
                        getInfo(0, informationService.
                                addInformation(0,1,recData.getLong("taskid"),recData.getLong("userid"))),
                                        recData.getLong("userid"));
        resultJson.put("message","成功");
        resultJson.put("code",flag);
        return resultJson;
    }

    //下线任务
    @PutMapping("/task/offline")
    public JSONObject offlineTask(@RequestBody JSONObject recData){
        JSONObject resultJson = new JSONObject();
        int flag= userAndTaskService.offlineTask(recData.getLong("userid"),recData.getLong("taskid"));
        if (flag==400){
            resultJson.put("code",flag);
            resultJson.put("message","失败");
            return resultJson;
        }
        resultJson.put("message","成功");
        resultJson.put("code",flag);
        return resultJson;
    }

    /**
     * 根据任务id返回任务发布者id，任务执行者id，以及其在该任务的状态
     * @param taskid
     * @return
     */
    @GetMapping("/user_task/get/{taskid}")
    public JSONObject getTaskId(@PathVariable("taskid") long taskid){
        List<UserTask> userTasks=userAndTaskService.getUserTaskId(taskid);
        log.info(userTasks.toString());
        if(userTasks.isEmpty())
        {
            JSONObject jsonObject=new JSONObject(true);
            jsonObject.put("code",400);
            jsonObject.put("message","失败");
            return jsonObject;
        }
        JSONObject jsonObject=new JSONObject(true);
        jsonObject.put("code",200);
        jsonObject.put("message","成功");
        jsonObject.put("data",userTasks);
        return jsonObject;
    }

    /**
     *根据角色获得该用户在该任务下的状态
     */
    @GetMapping("/user_task/getStatus/{userid}/{taskid}")
    public JSONObject getOneStatus(@PathVariable long userid,@PathVariable long taskid){
        int status= userAndTaskService.getOneStatus(userid,taskid);
        JSONObject resultJson = new JSONObject(true);
        if (status==400){
            resultJson.put("code",status);
            resultJson.put("message","失败");
            return resultJson;
        }
        JSONObject data = new JSONObject();
        data.put("status",status);
        resultJson.put("code",200);
        resultJson.put("message","成功");
        resultJson.put("data",data);
        return resultJson;
    }

}
